local desc = dofile "packages/seaborn/pkg/modes/huanhua/readme.md"

---@class HuanhuaLogic : GameLogic
local HuanhuaLogic = {}

function HuanhuaLogic:assignRoles()
  local room = self.room
  local n = #room.players
  for i = 1, n do
    local p = room.players[i]
    p.role = "hidden"
    p.role_shown = true
    room:broadcastProperty(p, "role")
    room:addSkill(Fk.skills["huanhua_rule"])
  end
  self.start_role = "hidden"
  -- for adjustSeats
  room.players[1].role = "lord"
end

function HuanhuaLogic:chooseGenerals()
  local room = self.room
  local lord = room:getLord()
  room.current = lord
  lord.role = "hidden"
  local players = room.players
  for _, p in ipairs(players) do
    local avatar = p._splayer:getAvatar()
    if avatar == "anjiang" then avatar = "guanyu" end
    local avatar_general = Fk.generals[avatar] or Fk.generals["guanyu"]
    room:setPlayerGeneral(p, avatar_general.name, true)
    room:broadcastProperty(p, "general")
    room:setPlayerProperty(p, "shield", 0)
    room:setPlayerProperty(p, "maxHp", 4)
    room:setPlayerProperty(p, "hp", 4)
  end
end

function HuanhuaLogic:broadcastGeneral() end
function HuanhuaLogic:attachSkillToPlayers() end

local getLogic = function()
  local h = GameLogic:subclass("HuanhuaLogic")
  for k, v in pairs(HuanhuaLogic) do
    h[k] = v
  end
  return h
end

local huanhua = fk.CreateGameMode{
  name = "huanhua",
  minPlayer = 2,
  maxPlayer = 10,
  logic = getLogic,
  rule = Fk.skills["huanhua_rule"]--[[@as TriggerSkill]],
  surrender_func = function (self, playedTime)
    local alive = Fk:currentRoom().alive_players
    return { { text = "left two", passed = (#alive == 2) } }
  end,
  winner_getter = function (self, victim)
    local room = victim.room
    local alive = table.filter(room.alive_players, function(p)
      return not p.surrendered
    end)
    if #alive > 1 then return "" end
    local true_name = alive[1]._splayer:getScreenName()
    for _, p in ipairs(room.players) do
      room:setPlayerProperty(p, "role", p._splayer:getScreenName())
    end
    return true_name
  end,
}

Fk:loadTranslationTable{
  ["huanhua"] = "幻化之战",
  [":huanhua"] = desc,

  ["left two"] = "场上仅剩两名角色",
}

return huanhua
